package com.aydqyy.springbootstart.controller;

import com.aydqyy.springbootstart.common.PageResult;
import com.aydqyy.springbootstart.dto.DeviceDTO;
import com.aydqyy.springbootstart.entity.Device;
import com.aydqyy.springbootstart.common.exception.Result;
import com.aydqyy.springbootstart.service.DeviceService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/device")
@Validated
@CrossOrigin
public class DeviceController {
    private DeviceService deviceService;

    @Autowired
    public DeviceController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @PostMapping("/add")
    public Result add(@RequestBody Map<String, String> params) {
        Device device = new Device();
        toDevice(params, device);
        this.deviceService.saveOrUpdate(device);
        return Result.success(device);
    }

    @PostMapping("/add2")
    public Result add2(@RequestBody @Validated DeviceDTO deviceDTO) {
        Device device = new Device();
        deviceDTO2Device(deviceDTO, device);
        this.deviceService.saveOrUpdate(device);
        return Result.success(device);
    }

    private void deviceDTO2Device(DeviceDTO deviceDTO, Device device) {
        device.setId(deviceDTO.getId());
        device.setName(deviceDTO.getName());
        device.setIp(deviceDTO.getIp());
        device.setOs(deviceDTO.getOs());
        device.setSystemUser(deviceDTO.getSystemUser());
        device.setSystemPwd(deviceDTO.getSystemPwd());
        device.setDbInfo(deviceDTO.getDbInfo());
        device.setDbUser(deviceDTO.getDbUser());
        device.setDbUserPwd(deviceDTO.getDbUserPwd());
        device.setDbPort(deviceDTO.getDbPort());
        device.setMemo(deviceDTO.getMemo());
    }

    // 根据传入的参数和设备对象，将参数设置到设备对象中
    private void toDevice(Map<String, String> params, Device device) {
        // 如果参数中存在id，则将id设置到设备对象中
        if (params.get("id") != null)
            device.setId(Integer.parseInt(params.get("id")));
        // 将name参数设置到设备对象中
        device.setName(params.get("name"));
        // 将IP参数设置到设备对象中
        device.setIp(params.get("ip"));
        // 将OS参数设置到设备对象中
        device.setOs(params.get("OS"));
        // 将systemUser参数设置到设备对象中
        device.setSystemUser(params.get("systemUser"));
        // 将systemPwd参数设置到设备对象中
        device.setSystemPwd(params.get("systemPwd"));
        // 将dbInfo参数设置到设备对象中
        device.setDbInfo(params.get("dbInfo"));
        // 将dbUser参数设置到设备对象中
        device.setDbUser(params.get("dbUser"));
        // 将dbUserPwd参数设置到设备对象中
        device.setDbUserPwd(params.get("dbUserPwd"));
        // 将dbPort参数设置到设备对象中
        device.setDbPort(params.get("dbPort"));
        // 将memo参数设置到设备对象中
        device.setMemo(params.get("memo"));
    }

    @GetMapping("/list")
    public Result list() {
        List<Device> devices = deviceService.list();
        Result result= Result.success(devices);
        return result;
    }

    @GetMapping("/list4page")
    public ResponseEntity<PageResult<Device>> listDevices(
            @RequestParam(name="pageNum",defaultValue = "1") int pageNum,
            @RequestParam(name="pageSize",defaultValue = "10") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword
    ) {
        Page<Device> page = deviceService.getDevicePage(pageNum, pageSize, keyword);
        PageResult<Device> result = new PageResult<Device>(page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        return ResponseEntity.ok(result);
    }

    @PatchMapping("/update")
    public Result update(@RequestBody Map<String, String> params) {
        Device currentDevice = deviceService.getById(params.get("id"));
        if (currentDevice == null) {
            return Result.error("设备不存在，无法更新");
        }
        Device device = new Device();
        toDevice(params, device);
        this.deviceService.saveOrUpdate(device);
        return Result.success(device);
    }

    @PatchMapping("/update2")
    public Result update2(@RequestBody @Validated DeviceDTO deviceDTO) {
        Device currentDevice = deviceService.getById(deviceDTO.getId());
        if (currentDevice == null) {
            return Result.error("设备不存在，无法更新");
        }
        Device device = new Device();
        deviceDTO2Device(deviceDTO, device);
        this.deviceService.saveOrUpdate(device);
        return Result.success(device);


    }

    @DeleteMapping("/delete")
    public Result delete(String id) {
        if (this.deviceService.removeById(id))
            return Result.success();
        return Result.error("删除失败！");
    }
}
